﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data.Linq;
using System.Data.SqlClient;
using BlogMVC.ModelEntity;
using System.Data;

namespace BlogMVC.Areas.Admin.Controllers
{
    public class ArticleController : Controller
    {
        DataBaseDataContext content = new DataBaseDataContext();
        //
        // GET: /Admin/Article/
        public ActionResult Index()
        {
            var article = content.Articles.ToList();
            return View(article);
        }
        private List<Article> List<T1>()
        {
            throw new NotImplementedException();
        }
        [HttpGet]
        public ActionResult Create()
        {
            return View();
        }
        [HttpPost]

        [ValidateInput(false)]
        public ActionResult Create(Article article,HttpPostedFileBase file)
        {
            try
            {
                //article.ImageLink = file.ToString();
                content.Articles.InsertOnSubmit(article);
                content.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch (Exception)
            {
                throw;
            }
        }
        public ActionResult Delete(int id)
        {
            var category = content.Articles.Where(o => o.ArticleId == id).Single();
            if (category == null)
             return RedirectToAction("Index");
            return View(category);
        }
        [HttpPost]
        public ActionResult Delete(int id, Article article)
        {
             article = content.Articles.Where(o => o.ArticleId == id).Single();
            try
            {
                if (article != null)
                {
                    var articlecommens = content.ArticleComments.Where(u => u.ArticleId == id);
                    content.ArticleComments.DeleteAllOnSubmit(articlecommens);
                    content.Articles.DeleteOnSubmit(article);
                    content.SubmitChanges();
                }
                return RedirectToAction("Index");
            }
            catch (Exception)
            {
                throw;
            }
        }
        public ActionResult Edit(int id)
        {
            var article = content.Articles.Where(o => o.ArticleId == id).Single();
            return View("Edit",article);
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(int id,int?y)
        {
            var a = content.Articles.Where(o => o.ArticleId == id).Single();
            if (a != null)
            {
                TryUpdateModel(a);
                content.SubmitChanges();
            }
            return RedirectToAction("Index");
        }
        public ActionResult Details(int id)
        {
            var  ar = content.Articles.Where(a=>a.ArticleId == id).Single();
            if (ar != null)
            {
                return View(ar);
            }
            return RedirectToAction("Index");
        }
    }
}
